/**
 * Created by haocongshun on 2016/8/25.
 */
var contextPath = "/";
$(function () {
    contextPath += window.location.pathname.split("/")[1];
    $("#toptitle").html("我的消息");
    getAll(1);
    bindJobTaskMsg();
    bindSystemMsg();
    bindEvaluate();
    bindGetAll();
});
function bindGetAll() {
    $("#getAllMsg").click(function () {
        getAll(1);
    });
}
function getAll(nowpage) {
    var url = contextPath+"/message/getall.wm";
    var dataAll = {};
    dataAll.flag="getall_stu";
    $.post(url,dataAll,function (result) {
        var newMessage="";
        var MSG = [];
        for(var i = 0;i < result.getall.evaluate.length;i++){
            var ceil = {};
            var temp = result.getall.evaluate[i].submit_time.replace(/-/g,'/');
            ceil.date = new Date(temp.substring(0,temp.length-2));
            ceil.content="<div class='panel king-panel1 m25'>"+
                "<div class='panel-heading king-panel-head1 king-panel-top1'>"+
                "<h3 class='panel-title'>作业评价</h3>"+
                "</div>"+
                "<div class='panel-body'>"+
                "<p>你的老师对你的作业“"+
                "<span>"+result.getall.evaluate[i].aWork.work_title+"</span>”做出了评价<br>评价内容："+
                result.getall.evaluate[i].evaluate+
                "</p>"+
                "<a href='"+contextPath+"/talk/talk.jsp?stu&id="+result.getall.evaluate[i].aWork.work_id+"'>【点击查看回复】</a>"+
                "<p class='messageDate'>"+temp.substring(0,temp.length-2)+"</p>"+
                "</div>"+
                "</div>";
            MSG.push(ceil);
        }
        for(var i = 0;i < result.getall.jobTask.length;i++){
            var ceil ={};
            var temp = result.getall.jobTask[i].jobTask_reportTime.replace(/-/g,'/');
            ceil.date = new Date(temp.substring(0,temp.length-2));
            ceil.content="<div class='panel king-panel1 m25'>"+
                "<div class='panel-heading king-panel-head1 king-panel-top1'>"+
                "<h3 class='panel-title'>" +
                (result.getall.jobTask[i].jobTask_title==null?"通知":"作业任务："+result.getall.jobTask[i].jobTask_title)+"</h3>"+
                "</div>"+
                "<div class='panel-body'>"+
                "<p>内容："+
                result.getall.jobTask[i].jobTask_content+
                "</p>"+
                "<p class='messageDate'>"+temp.substring(0,temp.length-2)+"</p>"+
                "</div>"+
                "</div>";
            MSG.push(ceil);
        }
        for(var i = 0;i < result.getall.systemMsg.length;i++){
            var ceil = {};
            var temp = result.getall.systemMsg[i].announce_date.replace(/-/g,'/');
            ceil.date = new Date(temp.substring(0,temp.length-2));
            ceil.content="<div class='panel king-panel1 m25'>"+
                "<div class='panel-heading king-panel-head1 king-panel-top1'>"+
                "<h3 class='panel-title'>系统通知</h3>"+
                "</div>"+
                "<div class='panel-body'>"+
                "<p>"+
                result.getall.systemMsg[i].systemMessage_content+
                "</p>"+
                "<p class='messageDate'>"+temp.substring(0,temp.length-2)+"</p>"+
                "</div>"+
                "</div>";
            MSG.push(ceil);
        }
        MSG.sort(function (a,b) {
            return b.date - a.date;
        });
        //分页
        var pagenum = Math.ceil(MSG.length/5);
        var pageHtml = "";
        for(var i = 5*(nowpage - 1);i < ((5*nowpage)<MSG.length?(5*nowpage):MSG.length);i++){
            newMessage+=MSG[i].content;
        }
        $("#MsgAll").html(newMessage);
        for(var i = 1; i <= pagenum;i++){
            pageHtml+="<li class='"+(i==nowpage?"active":"")+"'><a href='javascript:getAll("+i+");'>"+i+"</a></li>";
        }
        var toppage = "<li class='"+(nowpage==1?"disabled":"")+"'>"+
            "<a href='javascript:getAll("+(nowpage-1)+");' aria-label='Previous'>"+
            "<span aria-hidden='true'>«</span>"+
            "</a>"+
            "</li>";
        var bottompage = "<li class='"+(nowpage==pagenum?"disabled":"")+"'>"+
            "<a href='javascript:getAll("+(nowpage+1)+");' aria-label='Next'>"+
            "<span aria-hidden='true'>»</span>"+
            "</a>"+
            "</li>";
        $("#pageAll").html(toppage+pageHtml+bottompage);
        

    },"json");
}
/***
 * 绑定作业任务按钮
 */
function bindJobTaskMsg() {
    $("#jobTaskMsg").click(function () {
        getJobTaskMsg(1);
    });
}
/***
 * 获取作业任务通知信息
 */
function getJobTaskMsg(nowpage) {
    var dataJobTask = {};
    var url = contextPath+"/message/getJobTask.wm"
    $.post(url,dataJobTask,function (result) {
        var MSG = [];
        for(var i = 0;i < result.jobTask.length;i++){
            var ceil ={};
            var temp = result.jobTask[i].jobTask_reportTime.replace(/-/g,'/');
            ceil.date = new Date(temp.substring(0,temp.length-2));
            ceil.content="<div class='panel king-panel1 m25'>"+
                "<div class='panel-heading king-panel-head1 king-panel-top1'>"+
                "<h3 class='panel-title'>"+(result.jobTask[i].jobTask_title==null?"通知：":"作业任务："+result.jobTask[i].jobTask_title)+"</h3>"+
                "</div>"+
                "<div class='panel-body'>"+
                "<p>内容："+
                result.jobTask[i].jobTask_content+
                "</p>"+
                "<p class='messageDate'>"+temp.substring(0,temp.length-2)+"</p>"+
                "</div>"+
                "</div>";
            MSG.push(ceil);
        }
        var newMessage = "";
//分页
        var pagenum = Math.ceil(MSG.length/5);
        var pageHtml = "";
        for(var i = 5*(nowpage - 1);i < ((5*nowpage)<MSG.length?(5*nowpage):MSG.length);i++){
            newMessage+=MSG[i].content;
        }
        $("#MsgJobTask").html(newMessage);
        for(var i = 1; i <= pagenum;i++){
            pageHtml+="<li class='"+(i==nowpage?"active":"")+"'><a href='javascript:getJobTaskMsg("+i+");'>"+i+"</a></li>";
        }
        var toppage = "<li class='"+(nowpage==1?"disabled":"")+"'>"+
            "<a href='javascript:getJobTaskMsg("+(nowpage-1)+");' aria-label='Previous'>"+
            "<span aria-hidden='true'>«</span>"+
            "</a>"+
            "</li>";
        var bottompage = "<li class='"+(nowpage==pagenum?"disabled":"")+"'>"+
            "<a href='javascript:getJobTaskMsg("+(nowpage+1)+");' aria-label='Next'>"+
            "<span aria-hidden='true'>»</span>"+
            "</a>"+
            "</li>";
        $("#pageJobTask").html(toppage+pageHtml+bottompage);
        
     
    },"json");
}
/***
 * 绑定系统消息按钮
 */
function bindSystemMsg() {
    $("#systemMsg").click(function () {
        getSystemMsg(1);
    });
}
/***
 * 获取系统消息
 */
function getSystemMsg(nowpage) {
    var dataSystem = {};
    url = contextPath+"/message/getSystem.wm"
    $.post(url,dataSystem,function (result) {
        var MSG = [];
        for(var i = 0;i < result.systemMsg.length;i ++){
            var ceil = {};
            var temp = result.systemMsg[i].announce_date.replace(/-/g,'/');
            ceil.date = new Date(temp.substring(0,temp.length-2));
            ceil.content="<div class='panel king-panel1 m25'>"+
                "<div class='panel-heading king-panel-head1 king-panel-top1'>"+
                "<h3 class='panel-title'>系统通知</h3>"+
                "</div>"+
                "<div class='panel-body'>"+
                "<p>"+
                result.systemMsg[i].systemMessage_content+
                "</p>"+
                "<p class='messageDate'>"+temp.substring(0,temp.length-2)+"</p>"+
                "</div>"+
                "</div>";
            MSG.push(ceil);
        }
        var newMessage = "";
//分页
        var pagenum = Math.ceil(MSG.length/5);
        var pageHtml = "";
        for(var i = 5*(nowpage - 1);i < ((5*nowpage)<MSG.length?(5*nowpage):MSG.length);i++){
            newMessage+=MSG[i].content;
        }
        $("#MsgSystem").html(newMessage);
        for(var i = 1; i <= pagenum;i++){
            pageHtml+="<li class='"+(i==nowpage?"active":"")+"'><a href='javascript:getSystemMsg("+i+");'>"+i+"</a></li>";
        }
        var toppage = "<li class='"+(nowpage==1?"disabled":"")+"'>"+
            "<a href='javascript:getSystemMsg("+(nowpage-1)+");' aria-label='Previous'>"+
            "<span aria-hidden='true'>«</span>"+
            "</a>"+
            "</li>";
        var bottompage = "<li class='"+(nowpage==pagenum?"disabled":"")+"'>"+
            "<a href='javascript:getSystemMsg("+(nowpage+1)+");' aria-label='Next'>"+
            "<span aria-hidden='true'>»</span>"+
            "</a>"+
            "</li>";
        $("#pageSystem").html(toppage+pageHtml+bottompage);
        
       
    },"json");
}
/***
 * 绑定获取评价按钮
 */
function bindEvaluate() {
    $("#evaluateMsg").click(function () {
        getEvaluate(1);
    });
}
/***
 * 获取评价信息
 */
function getEvaluate(nowpage) {
    var dataEvaluate = {};
    var url =contextPath+"/message/getEvaluate.wm"
    $.post(url,dataEvaluate,function (result) {
        var MSG = [];
        for(var i = 0;i < result.evaluate.length;i++){
            var ceil = {};
            var temp = result.evaluate[i].submit_time.replace(/-/g,'/');
            ceil.date = new Date(temp.substring(0,temp.length-2));
            ceil.content="<div class='panel king-panel1 m25'>"+
                "<div class='panel-heading king-panel-head1 king-panel-top1'>"+
                "<h3 class='panel-title'>作业评价</h3>"+
                "</div>"+
                "<div class='panel-body'>"+
                "<p>你的老师对你的作业“"+
                "<span>"+result.evaluate[i].aWork.work_title+"</span>”做出了评价<br>评价内容："+
                result.evaluate[i].evaluate+
                "</p><a href='"+contextPath+"/talk/talk.jsp?stu&id="+result.evaluate[i].aWork.work_id+"'>【点击查看回复】</a>"+
                "<p class='messageDate'>"+temp.substring(0,temp.length-2)+"</p>"+
                "</div>"+
                "</div>";
            MSG.push(ceil);
        }
        var newMessage = "";

        var pagenum = Math.ceil(MSG.length/5);
        var pageHtml = "";
        for(var i = 5*(nowpage - 1);i < ((5*nowpage)<MSG.length?(5*nowpage):MSG.length);i++){
            newMessage+=MSG[i].content;
        }
        $("#MsgEvaluate").html(newMessage);
        for(var i = 1; i <= pagenum;i++){
            pageHtml+="<li class='"+(i==nowpage?"active":"")+"'><a href='javascript:getEvaluate("+i+");'>"+i+"</a></li>";
        }
        var toppage = "<li class='"+(nowpage==1?"disabled":"")+"'>"+
            "<a href='javascript:getEvaluate("+(nowpage-1)+");' aria-label='Previous'>"+
            "<span aria-hidden='true'>«</span>"+
            "</a>"+
            "</li>";
        var bottompage = "<li class='"+(nowpage==pagenum?"disabled":"")+"'>"+
            "<a href='javascript:getEvaluate("+(nowpage+1)+");' aria-label='Next'>"+
            "<span aria-hidden='true'>»</span>"+
            "</a>"+
            "</li>";
        $("#pageEvaluate").html(toppage+pageHtml+bottompage);


        
    },"json");
}
